package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        //转换为setmeal类型
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //注入setmeal表并获取主键
        setmealMapper.insert(setmeal);
        Long id = setmeal.getId();
        //获取菜品套餐关系
        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
        //判断
        if (setmealDishList == null || setmealDishList.size() == 0) {
            throw new DeletionNotAllowedException("套餐内必须包含菜品");
        }
        //加入主键
        for (SetmealDish setmealDish : setmealDishList) {
            setmealDish.setSetmealId(id);
        }
        //注入
        setmealDishMapper.insertBacrh(setmealDishList);
    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealDTO) {
        PageHelper.startPage(setmealDTO.getPage(),setmealDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder().id(id).status(status).build();
        setmealMapper.updateStatus(setmeal);
    }

    @Override
    @Transactional
    public void delete(List<Long> ids) {
        if (ids == null || ids.size() == 0) {
            throw new DeletionNotAllowedException("无法删除");
        }
        //查询是否是起售中
        List<Setmeal> list = setmealMapper.selectByIds(ids);
        System.out.println(list);
        for (Setmeal setmeal : list) {
            if (setmeal.getStatus() == 1) {
                throw new DeletionNotAllowedException("起售中的套餐不能删除");
            }
        }
        //删除
        setmealMapper.delete(ids);
        setmealDishMapper.deleteBySetmealIds(ids);
    }

    @Override
    public SetmealVO selectById(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        //查询setmeal表
        Setmeal setmeal = setmealMapper.selectById(id);
        //查询关系表
        List<SetmealDish> setmealDishList = setmealDishMapper.selectBySetmealId(id);
        //注入返回
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishList);
        return setmealVO;
    }

    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //修改setmeal表
        setmealMapper.update(setmeal);
        //修改关系表
        List<Long> ids = new ArrayList<>();
        ids.add(setmealDTO.getId());
        //先删除，后添加
        setmealDishMapper.deleteBySetmealIds(ids);
        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishList) {
            setmealDish.setSetmealId(setmealDTO.getId());
        }
        setmealDishMapper.insertBacrh(setmealDTO.getSetmealDishes());
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
